package org.example.pattern.chain.dao;

import org.example.pattern.chain.enums.GatewayEnum;
import org.example.pattern.chain.model.ChainConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * beyond u self and trust u self.
 *
 * @Author: lbc
 * @Date: 2023-05-12 10:54
 * @email: 594599620@qq.com
 * @Description: oh my god！ niu bi...
 */
public class ChainConfigManagerImpl implements ChainConfigManager {

    private static Map<Integer, ChainConfig> gatewayEntityMap = new HashMap<>();

    {
        GatewayEnum[] values = GatewayEnum.values();
        for (GatewayEnum value : values) {
            ChainConfig gatewayEntity = value.getGatewayEntity();
            gatewayEntityMap.put(gatewayEntity.getHandlerId(), gatewayEntity);
        }
        System.out.println("初始化成功！");
    }


    @Override
    public ChainConfig getChainConfig(Integer handlerId) {
        return gatewayEntityMap.get(handlerId);
    }

    @Override
    public ChainConfig getFirstChainConfig() {
        for (Map.Entry<Integer, ChainConfig> entry : gatewayEntityMap.entrySet()) {
            ChainConfig value = entry.getValue();
            //  没有上一个handler的就是第一个
            if (value.getPreHandlerId() == null) {
                return value;
            }
        }
        return null;
    }
}
